package org.lex.ftp.ui.explorer.actions;

import java.awt.event.ActionEvent;
import javax.swing.KeyStroke;

import org.lex.awt.SystemClipboard;
import org.lex.ftp.ui.explorer.FTPExplorer;
import org.lex.io.filesystem.FileNameUtilities;
import org.lex.net.ftp.FTPURL;
import org.lex.net.ftp.IFTPFile;
import org.lex.net.log.Log;


public class CopyURLAction extends FTPExplorerAction {
	public CopyURLAction(FTPExplorer view) {
		super(view);
		this.setName("Copy URL");
		this.setAcceleratorKey(KeyStroke.getKeyStroke("control U"));
	}

	public void actionPerformed(ActionEvent e) {
		Log log = this.getLogger();
		// no item was selected
		IFTPFile[] rows = explorer.getFileExplorer().getSelectedFiles();
		if (0 == rows.length)
			return;
		FTPURL getUrl = FTPURL.FAKE_URL;
		getUrl = explorer.getFTPModel().getFTPURL();
		log.misc(getUrl, rows.length + " URLs Copied to System Clipboard:");
		String host = getUrl.getServer();
		String pwd = explorer.getFTPModel().getCatalog();
		StringBuilder buffer = new StringBuilder();
		int count = rows.length - 1;
		for (int i = 0; i < rows.length; i++) {
			IFTPFile file = rows[i];
			String url = FileNameUtilities.getFullPath(pwd, file.getName(), '/');
			url = FileNameUtilities.getFullPath(host, url, '/');
			log.misc(getUrl, url);
			buffer.append(url);
			if (i < count)
				buffer.append('\n');
		}
		SystemClipboard.setClipboardContents(buffer.toString());
	}
}
